package g.beny.framework.web;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.nutz.dao.Cnd;
import org.nutz.mvc.Mvcs;
import org.nutz.mvc.view.JspView;
import org.nutz.mvc.view.UTF8JsonView;
/**
 * 基础服务类
 * 
 * @ClassName: ActionSupport
 * @Description: 基础服务类
 * @author gson
 * @date 2012-7-19 下午3:27:42
 * 
 */
public class ActionSupport {
	/**
	 * 动态组装查询条件
	 * methodName: getCnd
	 * Description:
	 * Created On: 2012-7-20 上午9:07:15
	 * @author gson
	 * <p>
	 * @return
	 */
	public Cnd getCnd(){
		return CndInjector.get(getRequest());
	}

	/**
	 * 判断是否是GET请求
	 * 
	 * @return
	 */
	public Boolean isGet() {
		return getRequest().getMethod().equalsIgnoreCase("GET");
	}

	/**
	 * 返回一个对象.这个对象会被序列化成json字符串输出
	 * 
	 * @param obj
	 * @return
	 */
	public UTF8JsonView jsonResult(Object obj) {
		UTF8JsonView jv = new UTF8JsonView(null);
		jv.setData(obj);
		return jv;
	}

	/**
	 * 返回操作执行结果状态
	 * 
	 * @param status
	 * @return
	 */
	public UTF8JsonView jsonResult(Boolean status) {
		UTF8JsonView jv = new UTF8JsonView(null);
		Map<String, Object> result = new HashMap<String, Object>();
		result.put("status", status);
		jv.setData(result);
		return jv;
	}

	/**
	 * 返回状态和消息
	 * 
	 * @param status
	 * @param info
	 * @return
	 */
	public UTF8JsonView jsonResult(Boolean status, String info) {
		UTF8JsonView jv = new UTF8JsonView(null);
		Map<String, Object> result = new HashMap<String, Object>();
		result.put("status", status);
		result.put("info", info);
		jv.setData(result);
		return jv;
	}
	
	/**
	 * 返回分页的datagrid结果
	 * @methodName
	 *  	jsonResultToGird
	 * @Description
	 * 
	 * @CreatedOn
	 * 		2012-7-20 上午9:17:14
	 * @author 
	 * 		gson
	 * @param 
	 * 		rows
	 * @param 
	 * 		total
	 * @return
	 */
	public UTF8JsonView jsonResultToGird(Object rows,int total){
		HashMap<String, Object> grid = new HashMap<String, Object>();
		grid.put("total", total);
		grid.put("rows", rows);
		return this.jsonResult(grid);
	}
	
	/**
	 * 返回不分页的datagrid结果
	 * @methodName: jsonResultToGird
	 * @Description:
	 * @CreatedOn: 2012-7-20 上午9:17:30
	 * @author gson
	 * <p>
	 * @param rows
	 * @return
	 */
	public UTF8JsonView jsonResultToGird(Object rows){
		return this.jsonResult(rows);
	}

	/**
	 * 返回jsp页面
	 * 
	 * @param path
	 * @return
	 */
	public JspView jspView(String path) {
		return new JspView(path);
	}

	/**
	 * 该方法相当执行request.setAttribute(key, value);
	 * 
	 * @param key
	 * @param value
	 */
	public void setReqAttr(String key, Object value) {
		getRequest().setAttribute(key, value);
	}

	/**
	 * 该方法相当执行session.setAttribute(key, value);
	 * 
	 * @param key
	 * @param value
	 */
	public void setSesAttr(String key, Object value) {
		getSession().setAttribute(key, value);
	}
	
	/**
	 * 该方法相当执行session.getAttribute(key);
	 * @methodName: getSesAttr
	 * @Description:
	 * @CreatedOn: 2012-8-25 下午3:54:38
	 * @author gson
	 * <p>
	 * @param key
	 * @return
	 */
	public Object getSesAttr(String key){
		return getSession().getAttribute(key);
	}

	/**
	 * 获取session
	 * 
	 * @return
	 */
	public HttpSession getSession() {
		return getRequest().getSession();
	}

	/**
	 * 获取Request
	 * 
	 * @return
	 */
	public HttpServletRequest getRequest() {
		return Mvcs.getReq();
	}

	/**
	 * 获取Response
	 * 
	 * @return
	 */
	public HttpServletResponse getResponse() {
		return Mvcs.getResp();
	}
}
